;(function(window, document, $) {
    var defaults = {
            id: 'jquery-notify',
            type: 'notice',
            timeout: 5000,
            speed : 250,
            dismiss: '(<span>click to close</span>)'
        },
        container = null;

    function getContainer() {
        if (!container)
            container = $('<div id="'+defaults.id+'"></div>')
                        .appendTo(document.body);
        return container
    }

    $.notify = function(msg, args) {
        args = $.extend({}, defaults, args);
        var t1, t2,
            $this = $('<div class="message ' + args.type + '"></div>')
                .append(msg)
            .hide()
            .appendTo(getContainer())
            .css('cursor', 'pointer')
            .click(function(){$this.close()})
            .slideDown(args.speed, function() {
                if (args.timeout)
                    t1 = window.setTimeout(function(){$this.close()}, args.timeout);
                if (args.dismiss) {
                    $('<span class=dismiss>&nbsp;' + args.dismiss + '</span>')
                        .css('visibility', 'hidden')
                        .appendTo($this);
                    t2 = window.setTimeout(function(){
                        $('span.dismiss', $this).css('visibility', 'visible')
                    }, 1000);
                }
            })
            .extend({
                close: function () {
                    if (t1)
                        window.clearTimeout(t1);
                    if (t2)
                        window.clearTimeout(t2);
                    $this.slideUp(args.speed, function(){$this.remove()});
                }
            });
        return $this;
    };

    $.notify.init = function(args) {
        $.extend(defaults, args);
    };

    (function(){
        function api(type, defaults) {
            $.notify[type] = function(msg, args) {
                if (typeof args == 'number')
                    args = {timeout: args};
                return $.notify(msg, $.extend({type:type}, defaults||{}, args));
            };
        }
        api('info');
        api('ok');
        api('alert');
        api('error', {timeout:0});
    })();

})(this, this.document, jQuery);

